<?php

  //
  // Memorii Web
  // content/folder.php
  //
  // Layout for a folder
  //
  
  if(file_exists('../../script/db/db.php'))
	  require_once('../../script/db/db.php');
	elseif(file_exists('../script/db/db.php'))
	  require_once('../script/db/db.php');
	else
  	require_once('script/db/db.php');	
  
  if(session_id()=='')
  	session_start();
  
  $folder_id = '0';
  if( isset($_SESSION['current_folder_id'])) {
  	$folder_id = $_SESSION['current_folder_id'];
  }
  if( isset($_POST['folder_id']) && $_POST['folder_id']!='' ) {
    $folder_id = $_POST['folder_id'];
  }
  $_SESSION['current_folder_id'] = $folder_id;
  
  $db_connection = db_connect();
  
  $folder_data = db_get_folder($db_connection, $folder_id);
?>
<div class="folder">
<?php
	if(isset($folder_data['parent_folder_id'])) {
?>
	<div class="lateral_bloc_actions">
		<div class="parent_folder_link" onclick="go_folder('<?php echo $folder_data['parent_folder_id']; ?>');">
			<span class="content_icon"><img src="img/folder/folder_50.png" alt="folder" /></span>
      <span>↑</span>
    </div>
    <div>
    	<button onclick="delete_folder();">Delete</button>
    </div>
	</div>
<?php
	}
?>
  <h2<?php if(isset($folder_data['name'])) echo ' class="editable" onclick="edit_folder_name();">'.$folder_data['name']; else echo '>Root folder'; ?></h2>
  <div class="folder_actions">
  	<button onclick="add_subfolder();">Add a new subfolder</button>
  	<button onclick="add_word();">Add a new word</button>
  </div>
  <div class="folder_content">
<?php
	$subfolders = db_get_subfolders($db_connection, $folder_id);
	
	foreach($subfolders as $subfolder) {
    echo '<div class="content_line" onclick="go_folder(\''.$subfolder['folder_id'].'\');">
      <span class="content_icon"><img src="img/folder/folder_50.png" alt="folder" /></span>
      <span>'.$subfolder['name'].'</span>
      <span class="content_action"><img src="img/folder/next.png" alt="folder" /></span>
    </div>';
	}

	$words = db_get_words($db_connection, $folder_id);
	
	foreach($words as $word) {
    echo '<div class="content_line" onclick="go_word(\''.$word['word_id'].'\')";>
      <span class="content_char">';
      if($word['word']=='')
      	echo '<span class="default">字</span>';
      else
      	echo $word['word'];
     	echo '</span>
      <span class="content_details">'.$word['reading'].': '.$word['meaning'].'</span>
      <span class="content_action"><img src="img/folder/next.png" alt="folder" /></span>
    </div>';
	}
?>
  </div>
</div>
<?php
	db_disconnect($db_connection);
?>